home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2003 November A / PCWK1103A.iso / ABBYY FineReader 7.0 PRO / data1.cab / pdf_ops.ps < prev    next >
Text File  |  2002-02-23  |  16KB  |  521 lines

  1. %    Copyright (C) 1994, 2000 Aladdin Enterprises.  All rights reserved.
  2. % This software is provided AS-IS with no warranty, either express or
  3. % implied.
  4. % This software is distributed under license and may not be copied,
  5. % modified or distributed except as expressly authorized under the terms
  6. % of the license contained in the file LICENSE in this distribution.
  7. % For more information about licensing, please refer to
  8. % http://www.ghostscript.com/licensing/. For information on
  9. % commercial licensing, go to http://www.artifex.com/licensing/ or
  10. % contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. % San Rafael, CA  94903, U.S.A., +1(415)492-9861.
  12.  
  13. % $Id: pdf_ops.ps,v 1.17.2.2 2002/02/22 19:45:55 ray Exp $
  14. % Definitions for most of the PDF operators.
  15.  
  16. .currentglobal true .setglobal
  17.  
  18. % Define pdfmark.  Don't allow it to be bound in.
  19. % Also don't define it in systemdict, because this leads some Adobe code
  20. % to think this interpreter is a distiller.
  21. % (If this interpreter really is a distiller, don't do this.)
  22. systemdict /pdfmark known not
  23.  { userdict /pdfmark { cleartomark } bind put } if
  24.  
  25. userdict /GS_PDF_ProcSet 127 dict dup begin
  26.  
  27. % ---------------- Abbreviations ---------------- %
  28.  
  29. /bdef { bind def } bind def
  30.  
  31. % ---------------- Graphics state stack ---------------- %
  32.  
  33. % PDF adds a number of parameters to the graphics state.
  34. % We implement this by pushing and popping a dictionary
  35. % each time we do a PDF gsave or grestore.
  36. % The keys in this dictionary are as follows:
  37. %    self            % identifies the dictionary as one of ours
  38. %    ClipRect        % (optional)
  39. %    Show
  40. %    TextSaveMatrix        % matrix at time of BT (iff within BT/ET)
  41. % (The following correspond directly to PDF state parameters.)
  42. %    AlphaIsShape
  43. %    FillConstantAlpha
  44. %    FillColor
  45. %    FillColorSpace
  46. %    FillOverprint
  47. %    SoftMask
  48. %    StrokeConstantAlpha
  49. %    StrokeColor
  50. %    StrokeColorSpace
  51. %    StrokeOverprint
  52. %    TextSpacing
  53. %    TextHScaling
  54. %    Leading
  55. %    TextFont
  56. %    TextLineMatrix
  57. %    TextMatrix
  58. %    TextRise
  59. %    TextRenderingMode
  60. %    WordSpacing
  61.  
  62. /nodict 1 dict def
  63. nodict /self { //nodict } executeonly put
  64. nodict readonly pop
  65.  
  66. /dictbeginpage {    % <initialdict> dictbeginpage -
  67.   //nodict 20 dict .copydict begin { def } forall
  68.   graphicsbeginpage textbeginpage
  69. } bdef
  70. /endpage {    % - endpage -
  71.   showpage end
  72. } bdef
  73.  
  74. /graphicsbeginpage {
  75.   initgraphics
  76.   currentdict /ClipRect .knownget { aload pop rectclip } if
  77.   0 g  0 G  false op  false OP  0 OPM
  78.   1 ca  1 CA  null SMask  false AIS  /Compatible BM  true TK
  79. } bdef
  80.  
  81. /gput        % <value> <key> gput -
  82.  { exch currentdict //nodict eq { /self dup load end 5 dict begin def } if
  83.         % If we're in a Level 1 system, we need to grow the
  84.         % dictionary explicitly.
  85.    currentdict length currentdict maxlength ge %eq
  86.     { currentdict dup length 3 mul 2 idiv 1 add dict .copydict end begin 
  87.     }
  88.    if def
  89.  } bdef
  90.  
  91. /q {
  92.   gsave //nodict begin
  93. } bdef
  94. % Some PDF files have excess Q operators!
  95. /Q {
  96.   currentdict /self .knownget { exec //nodict eq { end grestore } if } if
  97. } bdef
  98.  
  99. % ---------------- Color setting ---------------- %
  100.  
  101. /fcput        % <color> <colorspace> fcput -
  102.  { /FillColorSpace gput /FillColor gput
  103.  } bdef
  104. /scput        % <color> <colorspace> scput -
  105.  { /StrokeColorSpace gput /StrokeColor gput
  106.  } bdef
  107.  
  108. /csdevgray [/DeviceGray] readonly def
  109. /csdevrgb [/DeviceRGB] readonly def
  110. /csdevcmyk [/DeviceCMYK] readonly def
  111. /cspattern [/Pattern] readonly def
  112. /nullpattern1 mark
  113.    /PatternType 1 /PaintType 1 /TilingType 3 /BBox [0 0 0 0]
  114.    /XStep 1 /YStep 1 /PaintProc { }
  115. .dicttomark readonly def
  116. /nullpattern2 nullpattern1 dup length dict copy readonly def
  117.  
  118. % Each entry in the color space dictionary is a procedure of the form
  119. %    <cspace> -proc- <cspace> <initial-color>
  120. /CSdict mark
  121.   /DeviceGray { pop //csdevgray 0 } bind
  122.   /DeviceRGB { pop //csdevrgb [0 0 0] cvx } bind
  123.   /DeviceCMYK { pop //csdevcmyk [0 0 0 1] cvx } bind
  124.   /CIEBasedA { 0 } bind
  125.   /CIEBasedABC { [0 0 0] cvx } bind
  126.   /ICCBased { [ 1 index 1 oget /N get { 0 } repeat ] cvx } bind
  127.   /Separation { 1 } bind
  128.   /DeviceN {    % What is the correct value??
  129.     [ 1 index 1 get length { 1 } repeat ] cvx
  130.   } bind
  131.   /Indexed { 0 } bind
  132.   /Pattern {
  133.     dup type /nametype eq 1 index length 1 eq or {
  134.       pop //cspattern //nullpattern1 matrix makepattern
  135.     } {
  136.       //nullpattern2 matrix makepattern 1 index 1 get csset
  137.         % Stack: patternspace nullpattern basecolor basespace
  138.       pop [ 3 1 roll dup type /arraytype eq { aload pop } if
  139.       counttomark -1 roll ] cvx
  140.     } ifelse
  141.   } bind
  142. .dicttomark readonly def
  143. /csset            % <cspace> csset <color> <cspace>
  144.  { dup dup type /nametype ne { 0 get } if //CSdict exch get exec exch
  145.  } bdef
  146.  
  147. /g { //csdevgray fcput } bdef
  148. /G { //csdevgray scput } bdef
  149. /rg { 3 array astore cvx //csdevrgb fcput } bdef
  150. /RG { 3 array astore cvx //csdevrgb scput } bdef
  151. /k { 4 array astore cvx //csdevcmyk fcput } bdef
  152. /K { 4 array astore cvx //csdevcmyk scput } bdef
  153. /cs { csset fcput } bdef
  154. /CS { csset scput } bdef
  155. /ri { pop } bdef
  156. % We have to break up sc according to the number of operands.
  157. /sc1 { /FillColor gput } bdef
  158. /SC1 { /StrokeColor gput } bdef
  159. % We have to avoid storing into a color array associated with an outer
  160. % gsave level, so we do a kind of "copy on write".
  161. /sc* {
  162.   currentdict /FillColor .knownget {
  163.     astore pop
  164.   } {
  165.     /FillColor load length array astore cvx /FillColor gput
  166.   } ifelse
  167. } bdef
  168. /SC* {
  169.   currentdict /StrokeColor .knownget {
  170.     astore pop
  171.   } {
  172.     /StrokeColor load length array astore cvx /StrokeColor gput
  173.   } ifelse
  174. } bdef
  175.  
  176. % ---------------- Overprint/transparency setting ---------------- %
  177.  
  178. /op { /FillOverprint gput } bdef
  179. /OP { /StrokeOverprint gput } bdef
  180. /OPM {
  181.   /.setoverprintmode where { pop .setoverprintmode } { pop } ifelse
  182. } bdef
  183. /ca { /FillConstantAlpha gput } bdef
  184. /CA { /StrokeConstantAlpha gput } bdef
  185. /SMask { /SoftMask gput } bdef
  186. /AIS { /AlphaIsShape gput } bdef
  187. /BM {
  188.   /.setblendmode where {
  189.     pop [ exch dup type /nametype ne { aload pop } if /Normal ] {
  190.       { .setblendmode } .internalstopped not { exit } if pop
  191.     } forall
  192.   } {
  193.     pop
  194.   } ifelse
  195. } bdef
  196. /TK {
  197.   /.settextknockout where { pop .settextknockout } { pop } ifelse
  198. } bdef
  199.  
  200. % ---------------- Color installation ---------------- %
  201.  
  202. % Establish a given color (and color space) as current.
  203. /.settransparencyparams {    % <alpha> <smask> .settransparencyparams -
  204.   /.begintransparencymask where {
  205.     pop AlphaIsShape {
  206.       1 .setopacityalpha 0 .inittransparencymask exch .setshapealpha 1
  207.     } {
  208.       1 .setshapealpha 1 .inittransparencymask exch .setopacityalpha 0
  209.     } ifelse
  210.     % Set the soft mask by rendering the XObject.  Doing this every time
  211.     % is obviously very inefficient; we'll improve it later.
  212.     .settransparencymask    
  213.   } {
  214.     pop pop
  215.   } ifelse
  216. } bdef
  217. /.settransparencymask {        % <paramdict> <masknum> .settransparencymask -
  218.   exch dup null eq {
  219.     pop .inittransparencymask
  220.   } {
  221.     dup /Draw get exec
  222.   } ifelse
  223. } bdef
  224. % (Non-mask) images must execute setfillblend.
  225. /setfillblend {
  226.   FillOverprint setoverprint
  227.   FillConstantAlpha SoftMask .settransparencyparams
  228. } def
  229. /setfillstate {
  230.   FillColor FillColorSpace setgcolor setfillblend
  231. } def
  232. /setstrokestate {
  233.   StrokeColor StrokeColorSpace setgcolor StrokeOverprint setoverprint
  234.   StrokeConstantAlpha SoftMask .settransparencyparams
  235. } def
  236. /Cdict 15 dict dup begin    % <color...> <colorspace> -proc- -
  237.   /DeviceGray { pop setgray } bdef
  238.   /DeviceRGB { pop setrgbcolor } bdef
  239.   /DeviceCMYK { pop setcmykcolor } bdef
  240.   /CIEBasedA
  241.    { dup currentcolorspace eq { pop } { setcolorspace } ifelse setcolor } bdef
  242.   /CIEBasedABC /CIEBasedA load def
  243.   /CIEBasedDEF /CIEBasedA load def
  244.   /CIEBasedDEFG /CIEBasedA load def
  245.   /ICCBased /CIEBasedA load def
  246.   /Separation /CIEBasedA load def
  247.   /DeviceN /CIEBasedA load def
  248.   /Indexed /CIEBasedA load def
  249.   /Pattern
  250.    { dup currentcolorspace eq { pop } { setcolorspace } ifelse
  251.      dup /Matrix .knownget not { matrix } if
  252.      gsave DefaultMatrix setmatrix makepattern grestore setcolor
  253.    } bdef
  254. end def
  255. /setgcolor    % (null | <color...>) <colorspace> setgcolor -
  256.  { 1 index null eq
  257.     { pop pop }
  258.     { dup 0 get //Cdict exch get exec }
  259.    ifelse
  260.  } bdef
  261. /fsexec        % <fillop|strokeop> fsexec -
  262.  {        % Preserve the current point, if any.
  263.     { currentpoint } stopped
  264.     { $error /newerror false put   cvx exec }
  265.     { 3 -1 roll cvx exec moveto }
  266.    ifelse
  267.  } bdef
  268.  
  269. % ---------------- Path painting and clipping ---------------- %
  270.  
  271. /S { setstrokestate /stroke fsexec } bdef
  272. /f { setfillstate /fill fsexec } bdef
  273. /f* { setfillstate /eofill fsexec } bdef
  274. /n { newpath } bdef        % don't allow n to get bound in
  275. /s { closepath S } bdef
  276. /B { gsave setfillstate fill grestore S } bdef
  277. /b { closepath B } bdef
  278. /B* { gsave setfillstate eofill grestore S } bdef
  279. /b* { closepath B* } bdef
  280.  
  281. % Clipping:
  282.  
  283. /Wdict 4 dict dup begin
  284. /S { gsave setstrokestate stroke grestore n } bdef
  285. /f { gsave setfillstate fill grestore n } bdef
  286. /f* { gsave setfillstate eofill grestore n } bdef
  287. /n { end clip newpath } bdef
  288. end readonly def
  289. /W { //Wdict begin } bdef
  290. /W*dict 4 dict dup begin
  291. Wdict { def } forall
  292. /n { end eoclip newpath } bdef
  293. end readonly def
  294. /W* { //W*dict begin } bdef
  295.  
  296. % ---------------- Text control ---------------- %
  297.  
  298. /textbeginpage
  299.  { /TextSpacing 0 def        % 0 Tc
  300.    /TextLeading 0 def        % 0 TL
  301.    /TextRenderingMode 0 def    % 0 Tr
  302.    /TextRise 0 def        % 0 Ts
  303.    /WordSpacing 0 def        % 0 Tw
  304.    /TextHScaling 1.0 def    % 100 Tz
  305.    /TextFont null def
  306.    /Show { showfirst } def
  307.  } bdef
  308.  
  309. % Contrary to the statement in the PDF manual, BT and ET *can* be nested,
  310. % if the CharProc for a Type 3 font does a BT/ET itself.
  311. % Since we always call the CharProc inside a q/Q, we simply ensure that
  312. % the text state is saved and restored like the rest of the extended
  313. % graphics state.
  314.  
  315. /settextmatrix {
  316.   TextMatrix concat
  317.   TextHScaling 1 ne { TextHScaling 1 scale } if
  318.   TextRise 0 ne { 0 TextRise translate } if
  319.   TextFont dup null eq { pop } { setfont } ifelse
  320. } bdef
  321. /settextstate {
  322.     % The text state can be set even outside BT/ET.
  323.   currentdict /TextSaveMatrix known {
  324.     TextSaveMatrix setmatrix settextmatrix
  325.   } if
  326. } bdef
  327. /settextposition {
  328.         % Update the TextMatrix translation.
  329.   gsave TextSaveMatrix setmatrix
  330.   currentpoint TextMatrix 4 2 getinterval astore pop
  331.         % We would like to do "grestore currentpoint translate"
  332.         % here, but some PDF files set a singular text matrix
  333.         % (0 0 0 0 <x> <y> Tm), so we can't do this.
  334.   TextTempMatrix identmatrix setmatrix currentpoint
  335.   grestore
  336.   TextTempMatrix currentmatrix 4 2 getinterval astore pop
  337.   TextTempMatrix setmatrix
  338. } bdef
  339.  
  340. /BT {
  341.   currentdict /TextLineMatrix .knownget
  342.     { identmatrix pop TextMatrix identmatrix pop }
  343.     { matrix /TextLineMatrix gput matrix /TextMatrix gput }
  344.   ifelse
  345.   { showfirst } /Show gput
  346.   currentdict /TextSaveMatrix .knownget not {
  347.     matrix dup /TextSaveMatrix gput
  348.   } if currentmatrix pop settextmatrix
  349.   matrix /TextTempMatrix gput        % see settextposition
  350. } bdef
  351. /ET {
  352.   TextRenderingMode 4 ge { clip newpath } if
  353.   TextSaveMatrix setmatrix
  354.   currentdict /TextSaveMatrix undef
  355. } bdef
  356. /Tc { /TextSpacing gput { showfirst } /Show gput } bdef
  357. /TL { /TextLeading gput } bdef
  358. /Tr { /TextRenderingMode gput { showfirst } /Show gput } bdef
  359. /Ts { /TextRise gput settextstate } bdef
  360. /Tw { /WordSpacing gput { showfirst } /Show gput } bdef
  361. /Tz { 100 div /TextHScaling gput settextstate} bdef
  362.  
  363. % ---------------- Font control ---------------- %
  364.  
  365. /Tf {        % <font> <scale> Tf -
  366.   dup 1 eq { pop } { scalefont } ifelse
  367.   /TextFont gput settextstate
  368. } bdef
  369.  
  370. % Read a CFF font.
  371. /FRD        % <resname> <file> FRD -
  372.  { /FontSetInit /ProcSet findresource begin true ReadData
  373.  } bdef
  374.  
  375. % Copy a font, removing its FID.  If changed is true, also remove
  376. % the UniqueID and XUID, if any.  If the original dictionary doesn't have
  377. % the keys being removed, don't copy it.
  378. /.copyfontdict        % <font> <changed> .copyfontdict <dict>
  379.  { 1 index /FID known
  380.    1 index { 2 index /UniqueID known or 2 index /XUID known or } if
  381.     {        % We add 1 to the length just in case the original
  382.         % didn't have a FID.
  383.       exch dup length 1 add dict exch
  384.        {        % Stack: changed newfont key value
  385.      1 index /FID eq 4 index
  386.       { 2 index /UniqueID eq or 2 index /XUID eq or }
  387.      if not { 3 copy put } if pop pop
  388.        }
  389.       forall exch
  390.     }
  391.    if pop
  392.  } bdef
  393.  
  394. % Insert a new Encoding or Metrics into a font if necessary.
  395. % Return a possibly updated font, and a flag to indicate whether
  396. % the font was actually copied.
  397. /.updatefontmetrics {    % <font> <Metrics|null> .updatefontmetrics
  398.             %   <font'> <copied>
  399.   dup null ne {
  400.     exch true .copyfontdict dup /Metrics 4 -1 roll put true
  401.   } {
  402.     pop false
  403.   } ifelse
  404. } bdef
  405. /.updatefontencoding {    % <font> <Encoding|null> .updatefontencoding
  406.             %   <font'> <copied>
  407.   dup null ne { dup 2 index /Encoding get ne } { false } ifelse {
  408.     exch false .copyfontdict dup /Encoding 4 -1 roll put true
  409.   } {
  410.     pop false
  411.   } ifelse
  412. } bdef
  413. /.updatefont {        % <font> <Encoding|null> <Metrics|null> .updatefont
  414.             %   <font'> <copied>
  415.   3 -1 roll exch .updatefontmetrics
  416.             % Stack: enc|null font' copied
  417.   3 1 roll exch .updatefontencoding 3 -1 roll or
  418. } bdef
  419.  
  420. % ---------------- Text positioning ---------------- %
  421.  
  422. /Td {
  423.   TextLineMatrix transform TextLineMatrix 4 2 getinterval astore pop
  424.   TextLineMatrix TextMatrix copy pop settextstate
  425. } bdef
  426. /TD { dup neg /TextLeading gput Td } bdef
  427. /T* { 0 TextLeading neg Td } bdef
  428. /Tm {
  429.   TextLineMatrix astore TextMatrix copy pop settextstate
  430. } bdef
  431.  
  432. % ---------------- Text painting ---------------- %
  433.  
  434. /Vexch {
  435.   rootfont /WMode .knownget { 1 eq { exch } if } if
  436. } bind def
  437.  
  438. /textrenderingprocs [        % (0 is handled specially)
  439.     % Painting-only modes
  440.    { tf } { tS } { tB } { tn }
  441.     % Clipping modes
  442.    { gsave tf grestore tW }
  443.    { gsave tS grestore tW }
  444.    { gsave tB grestore tW }
  445.    { tW }
  446. ] readonly def
  447. /setshowstate
  448.  { WordSpacing 0 eq TextSpacing 0 eq and
  449.     { TextRenderingMode 0 eq
  450.        { { setfillstate show } }
  451.        { { false charpath textrenderingprocs TextRenderingMode get exec } }
  452.       ifelse
  453.     }
  454.     { TextRenderingMode 0 eq
  455.        { WordSpacing 0 eq
  456.           { { setfillstate TextSpacing 0 Vexch 3 -1 roll ashow } }
  457.       { TextSpacing 0 eq
  458.             { { setfillstate WordSpacing 0 Vexch 32 4 -1 roll widthshow } }
  459.             { { setfillstate WordSpacing 0 Vexch 32
  460.                  TextSpacing 0 Vexch 6 -1 roll awidthshow } }
  461.         ifelse
  462.       }
  463.      ifelse
  464.        }
  465.        { { WordSpacing TextSpacing
  466.             % Implement the combination of t3 and false charpath.
  467.             % Note that we must use cshow for this, because we
  468.             % can't parse multi-byte strings any other way.
  469.             % Stack: string xword xchar
  470.         { pop pop (x) dup 0 3 index put false charpath
  471.             % Stack: xword xchar ccode
  472.              3 copy 32 eq { add } { exch pop } ifelse 0 Vexch rmoveto pop
  473.         }
  474.        4 -1 roll cshow pop pop
  475.        textrenderingprocs TextRenderingMode get exec
  476.      }
  477.        }
  478.       ifelse
  479.     }
  480.    ifelse /Show gput
  481.  } bdef
  482. /showfirst { setshowstate Show } def
  483.  
  484. /Tj {
  485.   0 0 moveto Show settextposition
  486. } bdef
  487. /' { T* Tj } bdef
  488. /" { exch Tc exch Tw T* Tj } bdef
  489. /TJ {
  490.   0 0 moveto {
  491.     dup type /stringtype eq {
  492.       Show
  493.     } { -1000 div
  494.       currentfont /ScaleMatrix .knownget { 0 get mul } if
  495.       0 Vexch rmoveto
  496.     } ifelse
  497.   } forall settextposition
  498. } bdef
  499.  
  500. /tf { setfillstate currentpoint fill moveto } bdef
  501. /tn { currentpoint newpath moveto } bdef
  502. % For stroking characters, temporarily restore the graphics CTM so that
  503. % the line width will be transformed properly.
  504. /Tmatrix matrix def
  505. /tS
  506.  { setstrokestate
  507.    currentpoint //Tmatrix currentmatrix TextSaveMatrix setmatrix stroke
  508.    setmatrix moveto
  509.  } bdef
  510. /tB { gsave tf grestore tS } bdef
  511. % This does the wrong thing if there have been multiple text operations
  512. % within a single BT/ET pair, but it's a start.
  513. /tW { } bdef
  514.  
  515. end readonly put        % GS_PDF_ProcSet
  516.  
  517. .setglobal
  518.